package UCM;

import Source.CreateUser;
import Views.ViewCreateUser;

/**
 * The user case for create user.
 * @author Josh
 */
public class UCCreateUser implements UCController {

    private ViewCreateUser view;
    private CreateUser model = new CreateUser();

    /**
     * Constructor that initualised the view
     * @param view
     */
    public UCCreateUser(ViewCreateUser view) {
        this.view = view;
    }
    // <editor-fold defaultstate="collapsed" desc=" UML Marker "> 
    // #[regen=yes,id=DCE.AD87EB5B-A7A0-C362-62A0-6D83116B2F29]
    // </editor-fold> 

    /**
     * Precondition: The user is on the main UI, and is not logged on.

    User clicks create new account.
    System updates to the create user UI.
    User fills out the fields and clicks make account.
    System commits data.
    System logs the user in.
    System adds the user to the list of online users.
    System returns to the main UI and user is logged in.
    Postcondition: User logged in and on the main UI. Account has been created.

    Unsuccessful Outcomes:

    3a) The data entered by the user is inappropriate.
    System updates create user UI and explains errors to the User.
    4a) System can't commit data.
    System updates the UI informing the user that the service is currently unavailable.
    4b) System receives notice that an error has occurred.
    System updates the UI informing the user that there was an internal error.
     */
    public void run() {
        model.addDataToModel(view.getUser(), new String(view.getPassword()),
                new String(view.getConfirmPassword()), view.getFirstName(), view.getLastName(), view.getAddress());
        if (model.checkData()) {
            if (model.commitData()) {
                model.login();
                view.updateLogin();
            }
        }
    }
}

